<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="会员类型" prop="memberTypeList">
						<el-checkbox-group v-show="dataForm.id === 0"  v-model="dataForm.memberTypeList">
							<el-checkbox border size="medium" v-for="f in memberTypeList" :key="f.id" :label="f.id">{{ f.memberTypeName }}</el-checkbox>
						</el-checkbox-group>
						<el-checkbox-group v-show="dataForm.id !== 0"  v-model="dataForm.memberTypeList" :disabled="true">
							<el-checkbox border size="medium" v-for="f in memberTypeList" :key="f.id" :label="f.id">{{ f.memberTypeName }}</el-checkbox>
						</el-checkbox-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="10">
					<el-form-item label="钱" prop="exchangeAmount">
						<el-input v-model="dataForm.exchangeAmount" placeholder="兑换金额(消费多少金额兑换多少积分)"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="10">
					<el-form-item label="等于" prop="exchangeIntegral">
						<el-input v-model="dataForm.exchangeIntegral" placeholder="兑换积分(消费多少金额兑换多少积分)"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="4">积分</el-col>
			</el-row>
			<el-form-item label="启用积分清零" prop="integralClearRuleStatus">
				<el-radio v-model="dataForm.integralClearRuleStatus" label="1">启用</el-radio>
				 <el-radio v-model="dataForm.integralClearRuleStatus" label="2">停用</el-radio>
			</el-form-item>
			<!-- <el-form-item label="积分清零日期" prop="integralClearDate">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-select v-model="value" placeholder="请选择">
						    <el-option
						      v-for="item in options"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						    </el-option>
						  </el-select>
					</el-col>
				</el-row>
			</el-form-item> -->
			<el-form-item label="积分清零规则" prop="integralClearRule"><el-input v-model="dataForm.integralClearRule" placeholder="积分清零规则"></el-input></el-form-item>
			<el-form-item label="最低限额" prop="minQuota"><el-input v-model="dataForm.minQuota" placeholder="最低限额"></el-input></el-form-item>
			<el-form-item label="会员升级规则" prop="upgradeRule">
				<el-radio v-model="dataForm.upgradeRule" label="1">按历史积分升级</el-radio>
				 <el-radio v-model="dataForm.upgradeRule" label="2">按当前积分</el-radio>
			</el-form-item>
			<el-row :gutter="10">
				<el-col :span="8">
					<el-form-item label="积分从" prop="startIntegral"><el-input v-model="dataForm.startIntegral" placeholder="开始积分(积分区间可打折)"></el-input></el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="到" prop="endIntegral"><el-input v-model="dataForm.endIntegral" placeholder="结束积分(积分区间可打折)"></el-input></el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="可打" prop="discount"><el-input v-model="dataForm.discount" placeholder="结束积分(积分区间可打折)"></el-input></el-form-item>
				</el-col>
				<el-col :span="2">折</el-col>
			</el-row>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
export default {
	data() {
		return {
			memberTypeList:[],
			visible: false,
			dataForm: {
				id: 0,
				sellerId: '',
				memberTypeId: '',
				exchangeAmount: '',
				exchangeIntegral: '',
				integralClearRuleStatus: '1',
				integralClearRule: '',
				integralClearDate: '',
				minQuota: '',
				upgradeRule: '1',
				startIntegral: '',
				endIntegral: '',
				discount:'',
				memberTypeList:[]
			},
			dataRule: {
        memberTypeList:[{ required: true, message: '会员类型不能为空', trigger: 'blur' }],
				sellerId: [{ required: true, message: '租户不能为空', trigger: 'blur' }],
				memberTypeId: [{ required: true, message: '会员类型不能为空', trigger: 'blur' }],
				exchangeAmount: [],
				exchangeIntegral: [],
				integralClearRuleStatus: [],
				integralClearRule: [],
				integralClearDate: [],
				minQuota: [],
				upgradeRule: [],
				startIntegral: [],
				endIntegral: []
			}
		};
	},
	methods: {
		init(id) {
			//会员类型列表
			this.$http({
				url: this.$http.adornUrl('/store/tstoremembertype/list'),
				method: 'get',
				params: this.$http.adornParams({
					status:1,
          page:1,
          limit:100,
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.memberTypeList = data.page.list;
				} else {
					this.memberTypeList = [];
				}
			});

			this.dataForm.id = id || 0;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl(`/member/tstorememberrule/info/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.sellerId = data.tStoreMemberRule.sellerId;
							this.dataForm.memberTypeId = data.tStoreMemberRule.memberTypeId;
							this.dataForm.exchangeAmount = data.tStoreMemberRule.exchangeAmount;
							this.dataForm.exchangeIntegral = data.tStoreMemberRule.exchangeIntegral;
							this.dataForm.integralClearRuleStatus = data.tStoreMemberRule.integralClearRuleStatus.toString();
							this.dataForm.integralClearRule = data.tStoreMemberRule.integralClearRule;
							this.dataForm.integralClearDate = data.tStoreMemberRule.integralClearDate;
							this.dataForm.minQuota = data.tStoreMemberRule.minQuota;
							this.dataForm.upgradeRule = data.tStoreMemberRule.upgradeRule.toString();
							this.dataForm.startIntegral = data.tStoreMemberRule.startIntegral;
							this.dataForm.endIntegral = data.tStoreMemberRule.endIntegral;
							this.dataForm.memberTypeList = data.tStoreMemberRule.memberTypeList;
							this.dataForm.discount = data.tStoreMemberRule.discount;
						}
					});
				}
			});
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/member/tstorememberrule/${!this.dataForm.id ? 'save' : 'update'}`),
						method: 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							sellerId: this.dataForm.sellerId,
							memberTypeId: this.dataForm.memberTypeId,
							exchangeAmount: this.dataForm.exchangeAmount,
							exchangeIntegral: this.dataForm.exchangeIntegral,
							integralClearRuleStatus: this.dataForm.integralClearRuleStatus,
							integralClearRule: this.dataForm.integralClearRule,
							integralClearDate: this.dataForm.integralClearDate,
							minQuota: this.dataForm.minQuota,
							upgradeRule: this.dataForm.upgradeRule,
							startIntegral: this.dataForm.startIntegral,
							endIntegral: this.dataForm.endIntegral,
							memberTypeList: this.dataForm.memberTypeList,
							discount:this.dataForm.discount,
						})
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.visible = false;
									this.$emit('refreshDataList');
								}
							});
						} else {
							this.$message.error(data.msg);
						}
					});
				}
			});
		}
	}
};
</script>
